//
//  BN_SelectPersonViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-12.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_SelectPersonViewController.h"

@interface BN_SelectPersonViewController () {
    BN_PersonListData *personListData;
    BN_FaceChatViewController *faceChatVC_;
}
@end

@implementation BN_SelectPersonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"选择联系人";
    self.view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backChatHostPage)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // UITableView
    if (!self.selectPersonTableView) {
        self.selectPersonTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT) style:UITableViewStylePlain];
    }
    if (!personListData) {
        personListData = [[BN_PersonListData alloc]init];
    }
    switch (MAIN_VERSION) {

        case 6:
            self.selectPersonTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            
            self.selectPersonTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.selectPersonTableView.backgroundColor = [UIColor clearColor];
            self.selectPersonTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.selectPersonTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    if ([self.selectPersonTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.selectPersonTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    // 清楚多余边框
    [self setExtraCellLineHidden:self.selectPersonTableView];
    [personListData SelectPersonInfo:^(BOOL isSelect) {
        if (isSelect) {
            [self cancelHintView];
            [self.selectPersonTableView reloadData];
        }
    }];
    self.selectPersonTableView.delegate = personListData;
    self.selectPersonTableView.dataSource = personListData;
    [self.view addSubview:self.selectPersonTableView];
    // 按拼音排序
    
    // KCHAT_PUSHFACE
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushFaceChat:) name:@"KCHAT_PUSHFACE" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookPersonInfo:) name:K_CHAT_SELECTPERSON_LOOK_PERSONINFO object:nil];
    
    [self viewLoadHintView];
}

- (void)lookPersonInfo:(NSNotification *)notification {
    // BN_TeacherMeansViewController *teacherMeansVc = [[BN_TeacherMeansViewController alloc]init];
    // [self.navigationController pushViewController:teacherMeansVc animated:YES];
}

- (void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [tableView setTableFooterView:view];
}

// 返回聊聊主界面
- (void)backChatHostPage {
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

- (void)pushFaceChat:(NSNotification *)notification {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        [self.delegate pushFaceVc:notification.object];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    NSLog(@"聊聊选择练习人释放...");
}

@end
